﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Model.ViewModels.DTO;
using CoreCms.Net.Utility.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using CoreCms.Net.Services;

namespace CoreCms.Net.Web.WebApi.Controllers
{
    /// <summary>
    /// 品牌相关接口处理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DesignerController : ControllerBase
    {
        private IMapper _mapper;
        private readonly IHttpContextUser _user;

        private ICoreCmsGoodsCategoryServices _goodsCategoryServices;
        private IDesignerServices _designerServices;
        private ICoreCmsGoodsServices _goodsServices;

        /// <summary>
        /// 构造函数
        /// </summary>
        public DesignerController(IMapper mapper
            , IHttpContextUser user
                 , ICoreCmsGoodsCategoryServices goodsCategoryServices
                , ICoreCmsGoodsServices goodsServices
            , IDesignerServices designerServices
              



        )
        {
            _mapper = mapper;
            _user = user;
            _goodsCategoryServices = goodsCategoryServices;
            _designerServices = designerServices;
            _goodsServices = goodsServices;
        }


        [HttpGet]
        public async Task<WebApiCallBack> GetList()
        {
            var jm = new WebApiCallBack();

            jm.data = _designerServices.Query(); ;
            jm.status = true;

            return jm;
        }

        [HttpPost]
        public async Task<WebApiCallBack> GetDetial([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();

            jm.data = _designerServices.QueryById(entity.id);
            jm.status = true;

            return jm;
        }


        /// <summary>
        /// 获取品牌
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<WebApiCallBack> GetAllCategories([FromQuery]int designerId)
        {
            var jm = new WebApiCallBack() { status = true };

            var data = _goodsServices.QueryListByClause(m => m.isMarketable == true && m.isDel == false && m.designerId == designerId)
                .Select(m => new
                {
                    m.goodsCategoryId,
                }).GroupBy(m => m.goodsCategoryId);
            var goodsCategory = _goodsCategoryServices.QueryListByClause(m => m.isShow == true );
            var relist = new List<CoreCmsGoodsCategory>();
            foreach (var item in data)
            {
                relist.Add(goodsCategory.Find(m => m.id == item.Key));
            }

          
            jm.status = true;
            jm.data = relist;

            return jm;
        }


    }
}
